package fleet_management

import org.springframework.dao.DataIntegrityViolationException

class VehicleServicingController extends BaseController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def adminRole=session.getAttribute("adminRole")
		if(!adminRole.equals("Master Admin")){
			def vehicleservicingList=VehicleServicing.findAllByAdminRole(adminRole)
			if(vehicleservicingList)
				[vehicleServicingInstanceList: vehicleservicingList, vehicleServicingInstanceTotal:  vehicleservicingList.size()]
			else
				[vehicleServicingInstanceList: null, vehicleServicingInstanceTotal: VehicleServicing.count()]
		}else{
			[vehicleServicingInstanceList: VehicleServicing.list(params), vehicleServicingInstanceTotal: VehicleServicing.count()]
		}
	}

	def create() {
		def adminRole=session.getAttribute("adminRole")
		def vehicleList=null
		if(!adminRole.equals("Master Admin")){
			vehicleList=VehicleDatabase.findAllByAdminRole(adminRole)
		}else{
			vehicleList=VehicleDatabase.getAll()
		}
		[vehicleServicingInstance: new VehicleServicing(params),vehicleList:vehicleList]
	}

	def save() {
		def vehicleServicingInstance = new VehicleServicing(params)
		def adminRole=session.getAttribute("adminRole")
		if(adminRole=="Master Admin")
			vehicleServicingInstance.setAdminRole("Kathmandu")
		else
			vehicleServicingInstance.setAdminRole(adminRole)
		if (!vehicleServicingInstance.save(flush: true)) {
			render(view: "create", model: [vehicleServicingInstance: vehicleServicingInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
			vehicleServicingInstance.id
		])
		redirect(action: "show", id: vehicleServicingInstance.id)
	}

	def show() {
		def vehicleServicingInstance = VehicleServicing.get(params.id)
		if (!vehicleServicingInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[vehicleServicingInstance: vehicleServicingInstance]
	}

	def edit() {
		def vehicleServicingInstance = VehicleServicing.get(params.id)
		def adminRole=session.getAttribute("adminRole")
		def vehicleList=null
		if(!adminRole.equals("Master Admin")){
			vehicleList=VehicleDatabase.findAllByAdminRole(adminRole)
		}else{
			vehicleList=VehicleDatabase.getAll()
		}
		if (!vehicleServicingInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[vehicleServicingInstance: vehicleServicingInstance,vehicleList:vehicleList]
	}

	def update() {
		def vehicleServicingInstance = VehicleServicing.get(params.id)
		if (!vehicleServicingInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (vehicleServicingInstance.version > version) {
				vehicleServicingInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'vehicleServicing.label', default: 'VehicleServicing')]
						as Object[],
						"Another user has updated this VehicleServicing while you were editing")
				render(view: "edit", model: [vehicleServicingInstance: vehicleServicingInstance])
				return
			}
		}

		vehicleServicingInstance.properties = params
		def adminRole=session.getAttribute("adminRole")
		if(adminRole=="Master Admin")
			vehicleServicingInstance.setAdminRole("Kathmandu")
		else
			vehicleServicingInstance.setAdminRole(adminRole)
		if (!vehicleServicingInstance.save(flush: true)) {
			render(view: "edit", model: [vehicleServicingInstance: vehicleServicingInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
			vehicleServicingInstance.id
		])
		redirect(action: "show", id: vehicleServicingInstance.id)
	}

	def delete() {
		def vehicleServicingInstance = VehicleServicing.get(params.id)
		if (!vehicleServicingInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "list")
			return
		}

		try {
			vehicleServicingInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'vehicleServicing.label', default: 'VehicleServicing'),
				params.id
			])
			redirect(action: "show", id: params.id)
		}
	}
}
